<template>
  <div class="home">
    <div class="container">
      <el-form :inline="true" :model="form" :rules="rules" ref="form" class="form" label-width="120px">
        <el-form-item label="图书标准码" prop="book_no">
          <el-select filterable v-model="form.book_no" placeholder="请选择" @change="fullBook">
            <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.book_no"
              :value="item.book_no">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图书名称" prop="book_name">
          <el-input v-model="form.book_name" disabled placeholder="请输入图书名称"></el-input>
        </el-form-item>
        <el-form-item label="所需积分" prop="score">
          <el-input v-model="form.score" disabled placeholder="所需积分"></el-input>
        </el-form-item>
        <el-form-item label="会员号" prop="user_huiyuanhao">
          <el-select filterable v-model="form.user_huiyuanhao" placeholder="请选择" @change="fullUser">
            <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称" prop="user_name">
          <el-input v-model="form.user_name" disabled placeholder="请输入用户名称"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号" prop="user_phone">
          <el-input v-model="form.user_phone" disabled placeholder="请输入用户手机号"></el-input>
        </el-form-item>
        <el-form-item label="用户现有积分">
          <el-input v-model="account" disabled placeholder="用户现有积分"></el-input>
        </el-form-item>
        <el-form-item label="借阅天数">
          <el-input-number v-model="form.days" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="save">添加</el-button>

    </div>
  </div>
</template>

<script>

export default {
  name: 'AddBook',
  data() {
    return {
      form: {
        days:1
      },
      account:'',
      books:[],
      users:[],
      rules: {
        book_no: [
          { required: true, message: '请输入图书标准码', trigger: 'blur' },
        ],
        user_huiyuanhao:[
          { required: true, message: '请输入会员号', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.request.get("/api/book/list").then(res => {
      this.books = res.data;
    })

    this.request.get("/api/user/list").then(res => {
      this.users = res.data;
    })
  },
  methods: {
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.request.post("/api/borrow/save", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("添加成功");
            } else {
              this.$notify.error("添加失败");
            }
            this.form = {};
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    fullBook(){
      let book = this.books.find(v=>v.book_no === this.form.book_no);
      this.form.book_name = book.name;
      this.form.score = book.score;
    },
    fullUser(){
      let user = this.users.find(v=>v.username === this.form.user_huiyuanhao);
      this.form.user_name = user.name;
      this.form.user_phone = user.phone;
      this.account = user.account;
    }
  }
}
</script>


<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;

  .container {
    width: 800px;
    height: 500px;

    margin-top: 80px;
    background-color: #ffffff;
    border-radius: 25px;
    box-shadow: 0 3px 5px #ccc;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    .form {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
  }
}
</style>